from django.shortcuts import render
from rest_framework.views import APIView
from utils import qiniuyun,pager
from rest_framework.response import Response
from .models import Staff
from .serializer import Staff_Serializer
import operator
from dwebsocket import require_websocket
# Create your views here.

class Dim_Song_Name(APIView):
    def get(sels,request):
        dim_params = request.GET.get('dim_params','')
        if dim_params == '':
            return Response({'code':200,'msg':'查询成功','data':[]})
        staff = Staff.objects.filter(song_name__contains=str(dim_params))
        data_list = []
        for data in staff:
            data_list.append({
                "id":data.id,
                "song_name":data.song_name
            })
        # http://qmrcjhv9u.hb-bkt.clouddn.com/4dbb.jpg
        # http://qmrcjhv9u.hb-bkt.clouddn.com/4dbb.jpg
        return Response({'code':200,'msg':'查询成功','data':data_list})


class Qiniu(APIView):
    def get(self,request):
        try:
            song_id = request.GET.get('song_id','')
            page = request.GET.get('page', 0)
            page_size = request.GET.get('page_size', 1)
            if song_id == '':
                staff = Staff.objects.all()
                staff_serializer = Staff_Serializer(staff,many=True)
                data = sorted(staff_serializer.data, key=operator.itemgetter('release_time'),reverse=True)
                # 使用自定义分页器
                datapage = pager.mff_pager(data=data, page=page, page_size=page_size)
                return Response({'code':200,'msg':'查询成功','data':datapage,'total':len(data)})
            staff = Staff.objects.filter(id=song_id)
            staff_serializer = Staff_Serializer(staff,many=True)
            # 使用自定义分页器
            datapage = pager.mff_pager(data=staff_serializer.data, page=page, page_size=page_size)  
            return Response({'code':200,'msg':'查询成功','data':datapage,'total':len(staff_serializer.data)})
        except Exception as e:
            return Response({'code':503,'msg':'系统繁忙','error':str(e)})
        

    def post(self,request):
        try:
            song_name = request.data.get('song_name','')
            if not song_name:
                return Response({'code':403,'msg':'请输入曲谱名称'})
            author = request.data.get('author','')
            brief_introduction = request.data.get('brief_introduction','')
            path_list = request.data.get('path_list','')
            if not path_list:
                return Response({'code':403,'msg':'请上传图片'})
            if Staff.objects.filter(song_name=song_name).first():
                return Response({'code':403,'msg':'曲谱已存在'})
            images = ''
            number = 0
            for path in path_list:
                number += 1
                if number == len(path_list):
                    images += path
                else:
                    images += path + ','
        except Exception as e:
            return Response({'code':503,'msg':'参数错误','error':str(e)})
        try:
            staff = Staff(song_name=song_name,author=author,brief_introduction=brief_introduction,images=images).save()
            return Response({'code':200,'msg':'上传成功'})
        except Exception as e:
            return Response({'code':503,'msg':'上传失败','error':str(e)})

class Upload_Images(APIView):
    def post(self,request):
        try:
            image = request.FILES.get('image', None)
            img_name = qiniuyun.qiniu_upload(image)
            return Response({'code':200,'msg':'上传成功','img_name':img_name})
        except Exception as e:
            return Response({'code':503,'msg':'上传失败','error':str(e)})

class TestView(APIView):
    def get(self,request):
        page = int(request.GET.get('page',1))
        page_size = int(request.GET.get('size',1))
        data_start = (page-1) * page_size
        data_end = page * page_size
        staff = Staff.objects.all()[data_start:data_end]
        staff_serializer = Staff_Serializer(staff,many=True)
        return Response({"code":200,"msg":"成功","total":len(Staff.objects.all()),"data":staff_serializer.data})
